package net.example.activity.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.example.ggtalk.R;
import net.example.activity.view.sessionview.MessageItem;
import net.example.activity.view.sessionview.SlidingDeleteSlideView;
import net.example.utils.android.AndroidUI;

import java.util.HashMap;
import java.util.List;

public class GroupAdapter extends BaseAdapter {
	private Context context;
	private List<MessageItem> mMessageItems;
	private boolean isLongState;
	private HashMap<Integer, Boolean> checkedItemMap = new HashMap<Integer, Boolean>();
	private Drawable longStateItemCheckedBg, longStateItemNormalBg;

	public GroupAdapter(Context context, List<MessageItem> mMessageItems) {
		super();
		this.context = context;
		this.mMessageItems = mMessageItems;
		longStateItemCheckedBg = context.getResources().getDrawable(R.drawable.xlistview_check_bg);
		longStateItemNormalBg = context.getResources().getDrawable(R.drawable.xlistview_bg);
	}

	@Override
	public int getCount() {
		return mMessageItems.size();
	}

	@Override
	public Object getItem(int position) {
		return mMessageItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, final View convertView,ViewGroup parent) {

		SlidingDeleteSlideView slideView = (SlidingDeleteSlideView) convertView;
		try{
		ViewHolder holder;

		if (slideView == null) {
			View itemView = LayoutInflater.from(context).inflate(
					R.layout.group_item, parent, false);
			slideView = new SlidingDeleteSlideView(context);
			slideView.setContentView(itemView);
			holder = new ViewHolder(slideView);
			slideView.setTag(holder);
		} else {
			holder = (ViewHolder) slideView.getTag();
		}
		MessageItem item = mMessageItems.get(position);
		item.slideView = slideView;
		item.slideView.shrinkByFast();
		if (isLongState) {
			if (checkedItemMap.containsKey(position)) {
				holder.item_relayout
						.setBackgroundDrawable(longStateItemCheckedBg);
			} else {
				holder.item_relayout.setBackgroundColor(0xFFFFFFFF);
			}
		}
		holder.icon.setImageResource(R.drawable.group_ico);
		if (item.statusChanged) {
			AndroidUI.animationAlpha(holder.item_relayout);
			item.statusChanged = false;
		}else
//		if (item.isNewMsg) {
//			AndroidUI.shakeAnimationVertical(Animation.INFINITE, holder.item_relayout);去掉新消息抖动
//		} else{
//			AndroidUI.clearAnimation(holder.item_relayout);
//		}
		holder.icon.setImageResource(R.drawable.group_ico);
		holder.ct_status.setVisibility(View.GONE);
		holder.title.setText(item.title);
		holder.msg.setText(item.lastWord);
		}catch (Exception ex){ex.printStackTrace();}
		return slideView;
	}

	private static class ViewHolder {

		public RelativeLayout item_relayout;
		public ImageView icon,ct_status;
		public TextView title;
		public TextView msg;
		public TextView res;//未读消息
		public ViewGroup deleteHolder;

		ViewHolder(View view) {
			item_relayout = (RelativeLayout) view
					.findViewById(R.id.xlist_item_relayout);
			icon = (ImageView) view.findViewById(R.id.icon);
			ct_status = (ImageView) view.findViewById(R.id.ct_status);
			title = (TextView) view.findViewById(R.id.title);
			msg = (TextView) view.findViewById(R.id.msg);
			res = (TextView) view.findViewById(R.id.res);
			deleteHolder = (ViewGroup) view.findViewById(R.id.holder);
		}
	}

	public void setIsLongState(boolean isLongState) {
		this.isLongState = isLongState;
	}

	public void setCheckItemMap(HashMap<Integer, Boolean> checkedItemMap) {
		this.checkedItemMap = checkedItemMap;
	}

}
